<?php

class Application_Model_Users {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Application_Model_DbTable_Users();
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function createNew($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateUser($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = ?", $id);
        $fetchAll = $this->_dbtable->fetchAll($select);
        $formateData = $this->formateData($fetchAll[0]);
        return $formateData;
    }

    /**
     * 
     * @return array
     */
    public function fetchAll() {
        $select = $this->_dbtable->select();
        $fetchAll = $this->_dbtable->fetchAll($select);
        $array = array();
        foreach ($fetchAll as $value) {
            array_push($array, $this->formateData($value));
        }
        return $array;
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    private function formateData($data) {
        $optionsDb = new Application_Model_Options();
        $userType = $optionsDb->getByName("userType");
        $djson = json_decode($data['details']);
        $array = array(
            'id' => $data['id'],
            'email' => $data['email'],
            'name' => $djson->name,
            'details' => $djson->details,
            'profileUrl' => $djson->profileUrl,
            'user_type_id' => $data['user_type'],
            'user_type' => $userType['details'][$data['user_type']],
        );
        return $array;
    }

}

